package com.ruoyi.web.controller.User;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.CustomUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;
import java.util.Objects;

/**
 * 用户管理Controller
 * 
 * @author ruoyi
 * @date 2025-11-04
 */
@RestController
@RequestMapping("/user1/user2")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService loginService;

    /**
     * 查询用户管理列表
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomUser customUser)
    {
        startPage();
        List<CustomUser> list = userService.selectUserList(customUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomUser customUser)
    {
        List<CustomUser> list = userService.selectUserList(customUser);
        ExcelUtil<CustomUser> util = new ExcelUtil<CustomUser>(CustomUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户管理
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomUser customUser)
    {
        return toAjax(userService.insertUser(customUser));
    }

    /**
     * 修改用户管理
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomUser customUser)
    {
        return toAjax(userService.updateUser(customUser));
    }

    /**
     * 删除用户管理
     */
//    @PreAuthorize("@ss.hasPermi('user1:user2:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }


}
